package com.sykj.bsc.rwl.controller;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sykj.bsc.rwl.verification.VerificationCode;


@Controller
public class VerificationController {
	
	@RequestMapping("verificationCode")
	public void getImg(HttpServletRequest req,HttpServletResponse resp) throws IOException {
		//设置编码格式
		resp.setContentType("image/jpeg");
		//来几个随机数组成验证码
		Random r=new Random();
		String code="0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
		StringBuffer str=new StringBuffer();
		for(int i=0;i<4;i++) {
			str.append(code.charAt(r.nextInt(code.length())));
		}
		String data=str.toString();
		HttpSession session=req.getSession();
		session.setAttribute("data1", data);
		//接收图片 data是随机产生的4个验证码
		BufferedImage buff=VerificationCode.getVerifcationCode(data);
		//把图片写入前端中
		ImageIO.write(buff, "jpeg", resp.getOutputStream());
	}
}
